package edu.moravian.UI;

import edu.moravian.entity.Tower;
import edu.moravian.managers.TowerManager;
import edu.moravian.math.Point2D;
import edu.moravian.math.Vector2D;
import edu.moravian.player.PlayerInfo;
import edu.moravian.world.World2D;
import edu.moravian.world.WorldDescriptor;
import java.awt.Color;
import java.awt.Font;

/**
 *
 * @author ronaghan
 */
public class TowerUpgradeView extends GameView
{
    private Tower curUpgradeTower;
    private Tower curPlaceTower;
    
    private PlayAreaView playAreaView;
    private TowerManager towerManager;
    
    private WorldDescriptor sellBackBounds;
    
    public TowerUpgradeView(WorldDescriptor boundsDescriptor, PlayAreaView playAreaView, TowerManager towerManager)
    {
        super(boundsDescriptor);
        this.curUpgradeTower = null;
        this.curPlaceTower = null;
        
        Point2D upperRight = new Point2D(boundsDescriptor.getMaxX(), boundsDescriptor.getMinY() + boundsDescriptor.getHeight() * .1);
        this.sellBackBounds = new WorldDescriptor(boundsDescriptor.getLowerLeft(), upperRight);
        
        this.playAreaView = playAreaView;
        this.towerManager = towerManager;
    }

    
    public void setPlayAreaView(PlayAreaView playAreaView)
    {
        this.playAreaView = playAreaView;
    }
            
    public synchronized void draw(World2D w2d)
    {
        w2d.setColor(new Color(25, 25, 112));
        w2d.setOpacity(0.75f);
        w2d.drawCenteredRectangle(boundsDescriptor.getCenter(), boundsDescriptor.getWidth()-2, boundsDescriptor.getHeight()-2);
        w2d.setOpacity(1.0f);
        
        if(curUpgradeTower != null && curPlaceTower == null)
        {
            w2d.setColor(Color.GRAY);
            w2d.drawCenteredRectangle(boundsDescriptor.getCenter(), boundsDescriptor.getWidth()*.8, boundsDescriptor.getHeight()*.8);
            w2d.setColor(Color.BLACK);
            
            
            double x = boundsDescriptor.getMinX() + boundsDescriptor.getWidth() *.2;
            double y = boundsDescriptor.getMaxY() *.9;
            
           
            int fontSize = 20;
            Font oldFont = w2d.getFont();
            Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), fontSize);
            w2d.setFont(newFont);
          
            
            w2d.drawString("Tower: " + curUpgradeTower.getType(), new Point2D(x, y));
            
            y-=20;
            w2d.drawString("Cooldown Time: " + curUpgradeTower.getCooldownTime(), new Point2D(x, y));
            
            y-=20;
            w2d.drawString("SellBack Value: " + curUpgradeTower.getTowerProps().getSellBackCost(), new Point2D(x, y));
            
            y-=20;
            w2d.drawMultiLineString(curUpgradeTower.getTowerProps().getDescription(), new Point2D(x, y));
            
            
            
            
            w2d.setColor(Color.GRAY);
            w2d.setOpacity(1.0f);
            w2d.drawCenteredRectangle(sellBackBounds.getCenter(), sellBackBounds.getWidth()*.8, sellBackBounds.getHeight()*.8);
            
        
            w2d.setColor(Color.BLACK);

            w2d.drawMiddleCenteredString("Sell Back", sellBackBounds.getCenter());
            
            w2d.setFont(oldFont);
        
        }
        
        if(curPlaceTower != null)
        {
            w2d.setColor(Color.GRAY);
            w2d.drawCenteredRectangle(boundsDescriptor.getCenter(), boundsDescriptor.getWidth()*.8, boundsDescriptor.getHeight()*.8);
            w2d.setColor(Color.BLACK);
            
            
            double x = boundsDescriptor.getMinX() + boundsDescriptor.getWidth() *.2;
            double y = boundsDescriptor.getMaxY() *.9;
            
           
            int fontSize = 20;
            Font oldFont = w2d.getFont();
            Font newFont = new Font(oldFont.getName(), oldFont.getStyle(), fontSize);
            w2d.setFont(newFont);
          
            
            w2d.drawString("Tower: " + curPlaceTower.getType(), new Point2D(x, y));
            
            y-=20;
            w2d.drawString("Cost : " + curPlaceTower.getTowerProps().getCost(), new Point2D(x, y));
            
            y-=20;
            w2d.drawString("Fire Radius: " + curPlaceTower.getFireRadius(), new Point2D(x, y));
            
            
            y-=20;
            w2d.drawString("Cooldown Time: " + curPlaceTower.getCooldownTime(), new Point2D(x, y));
            
            y-=20;
            w2d.drawString("SellBack Value: " + curPlaceTower.getTowerProps().getSellBackCost(), new Point2D(x, y));
            
            y-=20;
            w2d.drawMultiLineString(curPlaceTower.getTowerProps().getDescription(), new Point2D(x, y));
            
            w2d.setFont(oldFont);
            

        }
        
        
    }

    public synchronized void setCurPlaceTower(Tower tower)
    {
        curPlaceTower = tower;
    }
    
    public synchronized void setCurUpgradeTower(Tower tower)
    {
        curUpgradeTower = tower;
    }
    
    public synchronized void handleMouseClick(Point2D location)
    {
        if (curUpgradeTower != null && sellBackBounds.inWorld(location))
        {
            PlayerInfo.setMonies(PlayerInfo.getMonies() + curUpgradeTower.getTowerProps().getSellBackCost());
            playAreaView.removeEntity(curUpgradeTower);
        }
    }

    @Override
    public void handleMouseMoved(Point2D location)
    {
        //throw new UnsupportedOperationException("Not supported yet.");
    }
}
